<template>
  <PageWrapper class="subdevide">
    <div class="devideall">
      <div class="header" @click.stop="goBack">
        <img src="../../../assets/images/goback.png" /> <span> {{ state.deviceInfo?.name }}</span>
        <a-tag
          class="tag"
          :color="
            state.deviceInfo.device_status == 1
              ? '#52C41A'
              : state.deviceInfo.device_status == 0
              ? '#FF5500'
              : '#FCA916'
          "
          >{{
            state.deviceInfo.device_status == 1
              ? '在线'
              : state.deviceInfo.device_status == 0
              ? '离线'
              : '未知'
          }}</a-tag
        >

        <a-alert
          v-if="
            state.deviceInfo.device_status == 0 &&
            state.deviceInfo.offline_reason &&
            state.deviceInfo.offline_reason.length > 0
          "
          :message="state.deviceInfo.offline_reason"
          banner
        />
      </div>
      <a-tabs
        v-model:activeKey="activeKey"
        @change="changeTab"
        style="padding-left: 15px; box-sizing: border-box"
      >
        <a-tab-pane key="1" tab="设备信息">
          <a-form :label-col="labelCol" :wrapper-col="wrapperCol" class="devide_info pad20">
            <a-row>
              <a-col :span="8">
                <a-form-item label="基础配置" class="title" />
                <a-form-item label="名称">
                  {{ state.deviceInfo?.name }}
                </a-form-item>
                <a-form-item label="自编码">
                  {{ state.deviceInfo?.code }}
                </a-form-item>
                <a-form-item label="品牌商">
                  {{ state.deviceInfo?.brand_name }}
                </a-form-item>
                <a-form-item label="型号">
                  {{ state.deviceInfo?.device_type_name }}
                </a-form-item>
                <a-form-item label="物模型">
                  {{ state.deviceInfo?.device_mode_name }}
                </a-form-item>
                <a-form-item label="设备分类">
                  {{ state.deviceInfo?.category_name }}
                </a-form-item>
                <a-form-item label="是否停用">
                  <a-tag :color="state.deviceInfo?.disable === 1 ? 'error' : 'success'">{{
                    state.deviceInfo?.disable === 1 ? '停用' : '启用'
                  }}</a-tag>
                </a-form-item>
                <a-form-item label="设备标签">
                  <a-tag
                    color="success"
                    v-for="(item, index) in state.deviceInfo?.tag_list"
                    :key="index"
                    >{{ item }}
                  </a-tag>
                </a-form-item>
                <a-form-item label="图片" name="bg_img">
                  <a-image :width="150" :src="state.deviceInfo?.bgimg" />
                </a-form-item>
              </a-col>
              <a-col :span="10">
                <a-form-item label="配置信息" class="title" />
                <!-- tcp_udp协议 -->
                <div v-if="state.deviceInfo?.connect_type == 'tcp_udp'">
                  <a-form-item label="序列号(设备编码)" name="device_serial">
                    {{ state.deviceInfo.device_serial || ' -' }}
                  </a-form-item>
                  <a-form-item label="类型" name="connect_mode">
                    {{ state.deviceInfo?.connect_mode || ' -' }}
                  </a-form-item>
                  <a-form-item label="IP地址" name="host">
                    {{ state.deviceInfo?.host || ' -' }}
                  </a-form-item>
                  <a-form-item label="端口" name="port">
                    {{ state.deviceInfo?.port || ' -' }}
                  </a-form-item>
                </div>
                <!-- modbus协议 -->
                <div v-if="state.deviceInfo.connect_type == 'modbus'">
                  <a-form-item label="IP地址" name="host">
                    {{ state.deviceInfo?.host || '/' }}
                  </a-form-item>
                  <a-form-item label="端口" name="port">
                    {{ state.deviceInfo?.port || ' -' }}
                  </a-form-item>
                  <a-form-item label="地址" name="address">
                    {{ state.deviceInfo?.address || ' -' }}
                  </a-form-item>
                  <a-form-item label="用户名" name="username">
                    {{ state.deviceInfo?.username || ' -' }}
                  </a-form-item>
                  <a-form-item label="密码" name="password">
                    {{ state.deviceInfo?.password || ' -' }}
                  </a-form-item>
                </div>

                <!-- odbc协议 -->
                <div v-if="state.deviceInfo.connect_type == 'odbc'">
                  <a-form-item label="IP地址" name="host">
                    {{ state.deviceInfo?.host || '/' }}
                  </a-form-item>
                  <a-form-item label="端口" name="port">
                    {{ state.deviceInfo?.port || ' -' }}
                  </a-form-item>
                  <a-form-item label="用户名" name="username">
                    {{ state.deviceInfo?.username || ' -' }}
                  </a-form-item>
                  <a-form-item label="密码" name="password">
                    {{ state.deviceInfo?.password || ' -' }}
                  </a-form-item>
                </div>

                <!-- http协议 -->
                <div
                  v-if="
                    state.deviceInfo.connect_type == 'http' ||
                    state.deviceInfo.connect_type == 'mqtt'
                  "
                >
                  <a-form-item label="IP地址" name="host">
                    {{ state.deviceInfo?.host || ' -' }}
                  </a-form-item>
                  <a-form-item label="端口" name="port">
                    {{ state.deviceInfo?.port || ' -' }}
                  </a-form-item>
                  <a-form-item label="用户名" name="username">
                    {{ state.deviceInfo?.username || ' -' }}
                  </a-form-item>
                  <a-form-item label="密码" name="password">
                    {{ state.deviceInfo?.password || ' -' }}
                  </a-form-item>
                  <a-form-item label="目录" name="url">
                    {{ state.deviceInfo?.url || ' -' }}
                  </a-form-item>
                </div>
                <!-- smb协议 -->
                <div v-if="state.deviceInfo.connect_type == 'smb'">
                  <a-form-item label="序列号(设备编码)" name="device_serial">
                    {{ state.deviceInfo?.device_serial }}
                  </a-form-item>
                  <a-form-item label="IP地址" name="host">
                    {{ state.deviceInfo?.host || ' -' }}
                  </a-form-item>
                  <a-form-item label="端口" name="port">
                    {{ state.deviceInfo?.port || ' -' }}
                  </a-form-item>
                  <a-form-item label="用户名" name="username">
                    {{ state.deviceInfo?.username || ' -' }}
                  </a-form-item>
                  <a-form-item label="密码" name="password">
                    {{ state.deviceInfo?.password || ' -' }}
                  </a-form-item>
                  <a-form-item label="目录" name="url">
                    {{ state.deviceInfo?.url || ' -' }}
                  </a-form-item>
                </div>
                <!-- s7协议 -->
                <div v-if="state.deviceInfo.connect_type == 's7'">
                  <a-form-item label="序列号(设备编码)" name="device_serial">
                    {{ state.deviceInfo?.device_serial || ' -' }}
                  </a-form-item>
                  <a-form-item label="IP地址" name="host">
                    {{ state.deviceInfo?.host || ' -' }}
                  </a-form-item>
                  <a-form-item label="端口" name="port">
                    {{ state.deviceInfo?.port || ' -' }}
                  </a-form-item>
                  <a-form-item label="机架" name="rack">
                    {{ state.deviceInfo?.rack || ' -' }}
                  </a-form-item>
                  <a-form-item label="槽位" name="slot">
                    {{ state.deviceInfo?.slot || ' -' }}
                  </a-form-item>
                </div>
                <!-- opcua协议 -->
                <div v-if="state.deviceInfo.connect_type == 'opcua'">
                  <a-form-item label="序列号(设备编码)">
                    {{ state.deviceInfo?.device_serial }}
                  </a-form-item>
                  <a-form-item label="IP地址" name="host">
                    {{ state.deviceInfo?.host || ' -' }}
                  </a-form-item>
                  <a-form-item label="端口" name="port">
                    {{ state.deviceInfo?.port || ' -' }}
                  </a-form-item>
                  <a-form-item label="用户名" name="username">
                    {{ state.deviceInfo?.username || ' -' }}
                  </a-form-item>
                  <a-form-item label="密码" name="password">
                    {{ state.deviceInfo?.password || ' -' }}
                  </a-form-item>
                  <a-form-item label="目录" name="url">
                    {{ state.deviceInfo?.url || ' -' }}
                  </a-form-item>
                </div>
                <a-form-item label="系数">
                  {{ state.deviceInfo?.coefficient || ' -' }}
                </a-form-item>
              </a-col>
              <a-col :span="6" class="editeBtn"
                ><a-button type="primary" size="small" @click="openEdit"
                  ><FormOutlined />编辑</a-button
                ></a-col
              >
            </a-row>
          </a-form>
        </a-tab-pane>
        <a-tab-pane key="2" tab="模型数据">
          <modeldataModel
            :devicemodeid="state.deviceInfo.device_mode_id"
            :id="state.deviceInfo.id"
          />
        </a-tab-pane>
        <a-tab-pane key="3" tab="上云状态">
          <!-- 上云状态弹框 -->
          <devicecludstatusModel ref="cloudstatusRef" />
        </a-tab-pane>
        <a-tab-pane key="4" tab="设备日志">
          <deviceLogModel />
        </a-tab-pane>
      </a-tabs>
    </div>
    <!-- 添加编辑页面 -->
    <addorEditeDialog ref="dialogRef" @success="submitSuccess" :isUpdate="isUpdate" />
  </PageWrapper>
</template>

<script lang="ts">
  import { defineComponent, reactive, ref, onMounted, Ref } from 'vue';
  import { PageWrapper } from '/@/components/Page';
  import { useRouter } from 'vue-router';
  import { FormOutlined } from '@ant-design/icons-vue';
  import devicecludstatusModel from '../../components/devicecludstatusModel.vue';
  import addorEditeDialog from './addorediteModel.vue';
  import deviceLogModel from '../../components/deviceLog/index.vue';
  import modeldataModel from '../../components/modeldataModel.vue';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { getbase64Img } from '/@/api/sys/upload';
  import { devicequerydevice, getDeviceModeList } from '/@/api/sys/gateway/gateway';
  export default defineComponent({
    components: {
      deviceLogModel,
      PageWrapper,
      FormOutlined,
      devicecludstatusModel,
      addorEditeDialog,
      modeldataModel,
    },
    setup() {
      const { createMessage } = useMessage();
      const router = useRouter();
      const dialogRef: Ref<any> = ref(null);
      const isUpdate = ref(false);
      const cloudstatusRef: Ref<any> = ref(null);
      let state: any = reactive({
        deviceInfo: '',
      });
      state.deviceInfo = JSON.parse(localStorage.getItem('deviceinfo') || '') || '';
      let activeKey = ref('1');

      async function getinfo() {
        devicequerydevice({
          id: state.deviceInfo.id,
        }).then((res) => {
          let rowdata = res;
          let tag_list: string[] = [];
          rowdata.tag_group.map((item: any) => {
            tag_list.push(item.tag_name);
          });
          rowdata.tag_list = tag_list;
          state.deviceInfo = rowdata;
          if (rowdata.bg_img) {
            getbase64Img({
              file_path: rowdata.bg_img,
            }).then((rex) => {
              state.deviceInfo.bgimg = rex.data;
            });
          }
          handleChangetype(rowdata);

          localStorage.setItem('deviceinfo', JSON.stringify(rowdata));
        });
      }
      function handleChangetype(e) {
        getDeviceModeList({
          device_type_id: e.device_type_id,
          page: 1,
          page_size: 99999999999,
        }).then((res: any) => {
          let data = res.list.find((val) => val.id == e.device_mode_id);
          state.deviceInfo.connect_type = data.connect_type;
        });
      }
      onMounted(() => {
        getinfo();
      });
      //切换tab
      function changeTab(e) {
        if (e == 3) {
          setTimeout(() => {
            cloudstatusRef.value.showTable(state.deviceInfo);
          }, 1000);
        }
      }
      //编辑
      function openEdit() {
        isUpdate.value = true;
        dialogRef.value.getediteDetail(state.deviceInfo);
      }
      //编辑成功回调
      function submitSuccess() {
        createMessage.success('修改成功');
        getinfo();
      }
      //返回上一页
      function goBack() {
        router.go(-1);
      }
      return {
        cloudstatusRef,
        changeTab,
        submitSuccess,
        activeKey,
        goBack,
        state,
        labelCol: { span: 8 },
        wrapperCol: { span: 15 },
        dialogRef,
        isUpdate,
        openEdit,
      };
    },
  });
</script>

<style lang="less" scoped>
  .pad20 {
    padding: 10px 20px;
    box-sizing: border-box;
  }
  .devide_info {
    .title {
      color: #303030;
      font-size: 14px;
      font-weight: 600;
      box-sizing: border-box;
      margin-bottom: 20px;
    }
    .editeBtn {
      display: flex;
      justify-content: flex-end;
    }
  }
  .subdevide {
    :deep(.ant-alert) {
      padding: 0px 15px !important;
      font-size: 16px;
    }
    :deep(.ant-tabs-bar) {
      border: none !important;
    }
    :deep(.ant-tabs-tab) {
      position: relative;
      padding: 12px 0px;
      box-sizing: border-box;
      font-size: 16px;
      &::after {
        content: '';
        width: 1px;
        height: 16px;
        background: #d9e0ed;
        border-radius: 1px;
        position: absolute;
        right: -12px;
        top: 0px;
        bottom: 0px;
        margin: auto;
      }
    }
    .devideall {
      background-color: #fff;
      .header {
        display: flex;
        align-items: center;
        font-weight: 600;
        background-color: #f0f2f5;
        padding-bottom: 10px;
        box-sizing: border-box;
        cursor: pointer;
        span {
          margin: 0px 6px;
          font-size: 20px;
        }

        img {
          width: 26px;
        }
        .tag {
          min-width: 72px;
          height: 28px;
          display: flex;
          align-items: center;
          justify-content: center;
          border-radius: 15px;
          font-size: 14px;
        }
      }
    }
  }
</style>
